<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <title>部门修改</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css">
    <link rel="stylesheet" href="../../../admin/css/dimples.css">
    <link rel="icon" href="../../../admin/images/favicon.ico" type="image/x-icon"/>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label required">编号</label>
                    <div class="layui-input-block">
                        <input type="text" name="deptId" lay-verify="required" required
                               autocomplete="off" placeholder="请输入编号" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="deptName" lay-verify="required" required
                               autocomplete="off" placeholder="请输入名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">上级部门</label>
                    <div class="layui-input-block">
                        <ul id="selectParent" name="parentId"></ul>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">负责人</label>
                    <div class="layui-input-block">
                        <!--<div id="leader" class="leader"></div>-->
                        <ul id="leader" name="leader"></ul>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-block">
                        <input type="text" name="email" lay-verify="email"
                               autocomplete="off" placeholder="请输入邮箱"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">联系方式</label>
                    <div class="layui-input-block">
                        <input type="text" name="mobile" lay-verify="phone"
                               autocomplete="off" placeholder="请输入联系方式"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input type="text" name="sort" lay-verify="number" autocomplete="off"
                               placeholder="请输入排序"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">详细地址</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入详细地址" name="address" id="address"
                                  class="layui-textarea"></textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="dept-update">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script src="../../../component/pear/dimples.js"></script>
<script>
    layui.use(['form', 'ajax', 'jquery', 'xmSelect'], function () {
        let $ = layui.jquery;
        let form = layui.form;
        let ajax = layui.ajax;
        let xmSelect = layui.xmSelect;

        let userSelect;

        ajax.send('/system/dept', {
            data: {
                deptId: parseParamByHref(location.href, 'deptId')
            }
        }, function (result) {
            let data = result.data;
            let sysDept = data[0];
            $("input[name = 'deptId']").attr("value", sysDept.deptId);
            $("input[name = 'deptName']").attr("value", sysDept.deptName);
            $("input[name = 'email']").attr("value", sysDept.email);
            $("input[name = 'mobile']").attr("value", sysDept.mobile);
            $("input[name = 'sort']").attr("value", sysDept.sort);
            $('#address').html(sysDept.address);

            userSelect = xmSelect.render({
                el: '#leader',
                paging: true,
                pageSize: 10,
                pageRemote: true,
                filterable: true,
                pageEmptyShow: false,
                prop: {
                    value: 'userId',
                    name: 'realName'
                },
                remoteMethod: function (val, cb, show, pageIndex) {
                    //val: 搜索框的内容, 不开启搜索默认为空, cb: 回调函数, show: 当前下拉框是否展开, pageIndex: 当前第几页
                    ajax.get('/system/user', {
                        data: {
                            pageNumber: pageIndex,
                            pageSize: 10,
                            realName: val
                        }
                    }, function (res) {
                        let d = res.data;
                        for (let i = 0; i < d.length; i++) {
                            d[i].selected = d[i].userId == sysDept.leader;
                        }
                        //回调需要两个参数, 第一个: 数据数组, 第二个: 总页码
                        cb(d, res.count)
                    }, function (error) {
                        console.log(error);
                        cb([], 0);
                    })
                },
                clickClose: true,
                theme: {
                    color: '#52c41a',
                },
                on: function (data) {
                    if (data.isAdd) {
                        return data.change.slice(0, 1)
                    }
                }
            });

            ajax.send('/system/dept/tree', {}, function (r) {
                let d = r.data;
                deptXmlSelect.update({
                    data: d
                });
                deptXmlSelect.setValue([sysDept.parentId]);
            });
        });


        let deptXmlSelect = xmSelect.render({
            el: '#selectParent',
            model: {label: {type: 'text'}},
            clickClose: true,
            tree: {
                show: true,
                strict: false,
                showLine: false,
                clickCheck: true,
                expandedKeys: [-1],
            },
            name: 'parentId',
            theme: {
                color: '#52c41a',
            },
            prop: {
                value: 'deptId',
                name: 'deptName',
                children: 'children'
            },
            height: 'auto',
            on: function (data) {
                if (data.isAdd) {
                    return data.change.slice(0, 1)
                }
            }
        });

        form.on('submit(dept-update)', function (data) {
            data.field.leader = userSelect.getValue()[0].userId;
            ajax.send('/system/dept', {
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
            }, function (result) {
                layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                    parent.render();
                });
            })
            return false;
        });
    })
</script>
<script>
</script>
</body>
</html>